$(function () {
	// 获取所有公众号
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				$("#allGzhCodes").append(
					"<option value=''>请选择公众号</option>"
				);
				for(var i=0;i<r.userGzhList.length;i++){
					$("#allGzhCodes").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});

	// 获取品牌列表
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/bbrandinfo/queryAll',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				for(var i=0;i<r.brandInfo.length;i++){
					$("#allBrandNames").append(
						"<option value=" + r.brandInfo[i].brandName +"></option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			gzhCode:null,
			brandId: null,
			brandName: null,
			articleId:null,
			userId:null,
			pushHeadId:null
		},
		showList: true
	},
	methods: {
		// 发送消息
		sendMessaging: function() {
			//debugger
			// 1、参数校验
			if (!this.validateParam()) {
				return;
			}

			$("#notice").html("<h4 style='color: red'>消息推送中...</h4>");
			debugger
			// 2、ajax 请求
			$.ajax({
				type: "POST",
				async: true,
				url:  'http://47.106.195.245:8095/UploadMsgPushBrandId',
				contentType: "application/json",
				dataType:"text",
				headers: {
					"profile": vm.q.gzhCode,
					"brandId": vm.q.brandId,
					"articleId": vm.q.articleId
				},
				success: function(r) {
					vm.q.pushHeadId = r;
					alert("消息推送返回ID:" + r);
					//this.showHeadId();
					$("#notice").html("");
				}
			});
		},
		// 发送消息(测试)
		sendMessagingTest: function() {
			// 1、参数校验
			if (!this.validateParam()) {
				return;
			}

			$("#notice").html("<h4 style='color: red'>消息推送中...</h4>");

			// 2、ajax 请求
			$.ajax({
				type: "POST",
				async: true,
				url: 'http://47.106.195.245:8095/UploadMsgPushBrandIdTest',
				contentType: "application/json",
				dataType:"text",
				headers: {
					 "userId": vm.q.userId,
					 "profile": vm.q.gzhCode,
					 "brandId": vm.q.brandId,
					 "articleId": vm.q.articleId

					//"userId": 4248286,
					//"profile": "prodbkwz",
					//"brandId": 39994,
					//"articleId": 6434892
				},
				success: function(r) {
					debugger
					vm.q.pushHeadId = r;
					alert("消息推送返回ID:" + r);
					//this.showHeadId();
					$("#notice").html("");
				}
			});
		},
		// 参数检验
		validateParam: function() {
			// 1、失去焦点
			$("#allGzhCodes").blur();
			$("#selectBrandName").blur();
			$("#articleId").blur();
			$("#userId").blur();

			// 2、非空判断
			let flag = false;
			if (this.q.gzhCode == null || this.q.gzhCode == '') {
				alert("公众号不能为空！");
				return flag;
			}
			if (this.q.brandId == null || this.q.brandId == '') {
				alert("品牌不能为空！");
				return flag;
			}
			if (this.q.articleId == null || this.q.articleId == '') {
				alert("文章ID不能为空！");
				return flag;
			}
			if (this.q.userId == null || this.q.userId == '') {
				alert("用户ID不能为空！");
				return flag;
			}
			return !flag;
		},
		// 品牌切换时，对应的 onchange 事件的函数
		updateBrand: function() {
			debugger
			if (vm.q.brandName == null || vm.q.brandName == '') {
				vm.q.brandId = null;
				return;
			}
			$.ajax({
				type: "POST",
				url: baseURL + 'sys/bbrandinfo/queryByName',
				contentType: "application/json",
				data: '{"brandName": "'+this.q.brandName+'"}',
				success: function(r) {
					if (r.code == 0) {
						// 获取品牌ID
						vm.q.brandId = r.brandInfo.id;
					}
				}
			});
		},
		showHeadId: function () {
			layer.open({
				type: 0,
				title: '推送消息返回ID',
				content: vm.q.pushHeadId,
				shadeClose: true
			});
		},
		query: function() {
			this.getPushHeadLog();
			this.getPushDetailLog();
			this.reload();
		},
		// 获取推送的头信息
		getPushHeadLog: function() {
			if (!vm.q.pushHeadId) {
				return;
			}
			$.ajax({
				type: "GET",
				url: baseURL + '/sys/pushheadlog/' + vm.q.pushHeadId,
				contentType: "application/json",
				success: function (r) {
					if (r.code == 0) {
						$("#pushName").text(r.pushHeadLog.pushName);
						$("#createTime").text(r.pushHeadLog.createTime);
						$("#endTime").text(r.pushHeadLog.endTime);
						$("#msgTypeName").text(r.pushHeadLog.msgTypeName);
						$("#successCount").text(r.pushHeadLog.successCount);
						$("#failCount").text(r.pushHeadLog.failCount);
						$("#errCount").text(r.pushHeadLog.errCount);
					}
				}
			});
		},
		// 获取推送详情
		getPushDetailLog: function() {
			$("#jqGrid").jqGrid({
				url: baseURL + 'sys/pushheadlog/detail',
				datatype: "json",
				colModel: [
					{ label: '用户ID', name: 'userId', index: 'user_id',align: "center", width:80 },
					{ label: '用户名称', name: 'userName', index: 'user_name',align: "center", width: 180 },
					{ label: '公众号', name: 'gzhName', index: 'gzh_name',align: "center", width: 100 },
					{ label: '消息类型', name: 'msgTypeName', index: 'msg_type_name',align: "center", width: 100 },
					{ label: '推送时间', name: 'pushTime', index: 'push_time',align: "center", width: 180 },
					{ label: '结果', name: 'result', index: 'result',align: "center", width: 80},
					{ label: '消息', name: 'msg', index: 'msg',align: "center", width: 600 },
					{ label: '任务名称', name: 'pushName', index: 'push_name',align: "center", width: 180},
				],
				viewrecords: true,
				height: 500,
				rowNum: 20,
				rowList : [20,50,100],
				rownumbers: true,
				rownumWidth: 25,
				autowidth:true,
				multiselect: false,
				pager: "#jqGridPager",
				jsonReader : {
					root: "page.list",
					page: "page.currPage",
					total: "page.totalPage",
					records: "page.totalCount"
				},
				postData: {'id': vm.q.pushHeadId},
				prmNames : {
					page:"page",
					rows:"limit",
					order: "order"
				},
				gridComplete:function(){
					//隐藏grid底部滚动条
					$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
					$("#gbox_jqGrid th").css("text-align", "center");
					$("#jqGrid").jqGrid("setGridWidth", $("#pushHeadtb").width());// 重置宽度
				}
			});
		},
		// 重新加载页面
		reload: function (event) {
			vm.showList = false;
			$("#jqGrid").jqGrid('setGridParam',{
				postData: {'id': vm.q.pushHeadId},
                page:1
            }).trigger("reloadGrid");
		},
	}
});